using System;
using Sce.Pss.HighLevel.GameEngine2D;
using Sce.Pss.HighLevel.GameEngine2D.Base;
using Sce.Pss.Core;

namespace RedSkies
{
	public class GravityInstance
	{
		protected double velocityForEachSecond;
		protected double currentVelocity;
		protected double currentSpeedY;
		protected double currentY;
		public long lastUpdatetime;
		public bool IsFalling {get;set;}
				
		public GravityInstance (double velocity)
		{
			this.velocityForEachSecond = velocity;
			currentVelocity = 0;
			
		}
		
		public void StartGravity(double currentspeedY, float yStart)
		{
			lastUpdatetime =  System.Environment.TickCount;
			this.currentSpeedY= currentspeedY;
			IsFalling=true;
			this.currentY=yStart;
		}
		
		public void StopGravity()
		{
			currentVelocity = 0;
		}
		
		public float CalculateY()
		{
			long currenttime = System.Environment.TickCount;
			double deltatime = currenttime-lastUpdatetime;
			lastUpdatetime= currenttime;
			if (deltatime==0)
			{
				deltatime=1;	
			}
			deltatime = deltatime/1000;
			currentVelocity = currentVelocity + (velocityForEachSecond* deltatime);
			
			double curSpeedYNow = currentSpeedY- (currentVelocity);
			
			if (curSpeedYNow<-25)
			{
				curSpeedYNow= -25;
			}
			
			currentY = currentY + curSpeedYNow;
			return (float)currentY;
		}		
	}
}

